package com.tidc.seaserverweb.controller;

import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.Task;
import com.tidc.seaservermodel.entity.UserTask;
import com.tidc.seaservermodel.exception.TidcAuthorityException;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.exception.TidcQuartzException;
import com.tidc.seaservermodel.properties.FileProperties;
import com.tidc.seaserverservice.service.SecondaryTaskService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/secondary/task")
//@CrossOrigin(origins = "*",maxAge = 3600,allowCredentials = "true")
public class SecondaryTaskController {

    @Resource
    private SecondaryTaskService secondaryTaskService;
    @Resource
    private FileProperties fileProperties;
    @PostMapping("/upload")
    public Fres<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        assert fileName != null;
        String fileType = fileName.substring(fileName.indexOf("."));
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");//去-
        File dest = new File(fileProperties.getPhotoFilePath() + uuid+fileType);
        if (!dest.getParentFile().exists()) {
            if (!dest.getParentFile().mkdirs()) {
                return Fres.badRequestParent(null,"请重新上传");
            }
        }
        file.transferTo(dest);
        return Fres.Success(fileProperties.getPhotoWebUrl()+uuid+fileType,"上传成功");
    }
    /**
     * 获取上级账号的发布任务列表
     *
     * @param pageSize
     * @param pageNum
     * @param authentication
     * @return
     * @throws TidcBadParent
     * @throws ParseException
     */
    @GetMapping("/list")
    public Fres<Map<Object, Object>> getTaskAll(@RequestParam("query") String query,@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum, Authentication authentication) throws TidcBadParent, ParseException {
        return secondaryTaskService.getTaskAll(query,pageSize, pageNum, authentication);
    }

    @PostMapping("/receive")
    public Fres<String> receiveTask(@RequestBody Task task, Authentication authentication) throws ParseException, SQLException, TidcBadParent, TidcAuthorityException, TidcQuartzException {
        return secondaryTaskService.receiveTask(task, authentication);
    }

    /**
     * 获取当前用户的任务列表
     *
     * @param pageSize
     * @param pageNum
     * @param authentication
     * @return
     */
    @GetMapping("/i/list")
    public Fres<Map<Object, Object>> getIReceiveTask(@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum, Authentication authentication) throws TidcBadParent {
        return secondaryTaskService.getIReceiveTask(pageSize, pageNum, authentication);
    }

    @GetMapping("/ongoing")
    public Fres<Map<Object, Object>> getOngoingTask(@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum, Authentication authentication) throws TidcBadParent {
        return secondaryTaskService.getOngoingTask(pageSize, pageNum, authentication);
    }

    @PostMapping("/finish")
    public Fres<String> finishTask(Authentication authentication, @RequestBody UserTask userTask) throws Exception {
        return secondaryTaskService.finishTask(authentication, userTask);
    }

    /**
     * 已完成和以放弃列表 1完成 2放弃 0等待
     * @param pageSize
     * @param pageNum
     * @param isType
     * @param authentication
     * @return
     * @throws TidcBadParent
     */
    @GetMapping("/completed")
    public Fres<Map<Object, Object>> completedTask(@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum, @RequestParam("isType") int isType, Authentication authentication) throws TidcBadParent {
        return secondaryTaskService.completedTask(pageSize,pageNum, isType, authentication);
    }
    @GetMapping("/id")
    public Fres<Task> getTaskInfo(@RequestParam("id")int id){
        return secondaryTaskService.getTaskInfo(id);
    }

}